/************************************************************************************
* SavedPlaylist.cpp                                                                 *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "SavedPlaylist.h"

#include "collector/Collector.h"

SavedPlaylist::SavedPlaylist(QString name,
                    QDateTime createTime,
                    QDateTime modTime,
                    MediaRating rating,
                    QString comment):
    m_playlistName(name),
    m_creationTime(createTime),
    m_lastModifiedTime(modTime),
    m_rating(rating),
    m_comment(comment),
    m_entries(0){
}


SavedPlaylist::~SavedPlaylist() {
    if(m_entries) {
        clear();
        delete m_entries;
    }
}


QDateTime SavedPlaylist::creationTime() {
    return m_creationTime;
}

QDateTime SavedPlaylist::lastModifiefTime() {
    return m_lastModifiedTime;
}

QString SavedPlaylist::playlistName() {
    return m_playlistName;
}

MediaRating SavedPlaylist::rating() {
    return m_rating;
}


QString SavedPlaylist::comment() {
    return m_comment;
}

QList<PlaylistEntry *> *SavedPlaylist::entries() {
    return m_entries;
}


void SavedPlaylist::setComment(QString comment) {
    m_comment = comment;
}


void SavedPlaylist::setRating(MediaRating rating) {
    m_rating  = rating;
}


void SavedPlaylist::setPlaylistEntries(QList<PlaylistEntry *> *entries) {
    if(m_entries) {
        clear();
        delete m_entries;
    }
    m_entries = entries;
}


int SavedPlaylist::rowCount(const QModelIndex &parent) const {
    return m_entries->size();
}


int SavedPlaylist::columnCount(const QModelIndex &parent) const {
    return 5;

}

QVariant SavedPlaylist::data(const QModelIndex &index, int role) const {
    if(m_entries != NULL && index.isValid()) {
        PlaylistEntry *entry = m_entries->at(index.row());
        CollectorItem *item = Collector::get()->item(entry->trackId());
        switch(index.column()) {
        case 0: return item->trackId();
        case 1: return item->title();
        case 2: return item->artist();
        case 3: return item->album();
        case 4: return item->rating();
        default: return "###";
        }
    }
    return QVariant();
}

QVariant SavedPlaylist::headerData(int section, Qt::Orientation orientation, int role) const {

    switch(section) {
        case 0: return tr("Track Id");
        case 1: return tr("Title");
        case 2: return tr("Artist");
        case 3: return tr("Album");
        case 4: return tr("Rating");
        default: return "###";
    }
    return QVariant();

}

QModelIndex SavedPlaylist::index(int row, int column, const QModelIndex &parent) const {
    createIndex(row, column);
}

QModelIndex SavedPlaylist::parent(const QModelIndex &child) const {
    return QModelIndex();
}

bool SavedPlaylist::hasChildren(const QModelIndex &parent) const {
    if(! parent.isValid()) {
        return  true;
    }
    return true;
}


void SavedPlaylist::clear() {
    for(int i = 0; i < m_entries->size(); ++i) {
        delete m_entries->at(i);
    }
    m_entries->clear();
}


void SavedPlaylist::addToPlaylist(QString trackId) {
    int index = m_entries->size()?m_entries->size() - 1 : 0;
    beginInsertRows(QModelIndex(), index, index);
    m_entries->append(new PlaylistEntry(trackId, index));
    endInsertRows();
}


void SavedPlaylist::removeFromPlaylist(int index) {
    if(index < m_entries->size() && index >= 0) {
        beginRemoveRows(QModelIndex(), index, index);
        m_entries->removeAt(index);
        endRemoveRows();
    }
}



void SavedPlaylist::moveUp(int index) {
    if(index > 0 && index < m_entries->size()) {
        beginMoveRows(QModelIndex(), index, index, QModelIndex(), index - 1);
        m_entries->swap(index, index - 1);
        endMoveRows();
    }
}



void SavedPlaylist::moveDown(int index) {
    if(index >= 0 && index < m_entries->size() - 1) {
        beginMoveRows(QModelIndex(),
                      index,
                      index,
                      QModelIndex(),
                      index + 2); //i dont know how this works
        m_entries->swap(index, index + 1);
        endMoveRows();
    }
}


bool SavedPlaylist::operator ==(SavedPlaylist &entry) {
    return this->m_playlistName == entry.playlistName();
}
